package com.hc.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@SpringBootConfiguration
@MapperScans({@MapperScan(basePackages = "com.hc.nong", sqlSessionFactoryRef = "nongFactory"), @MapperScan(basePackages = "com.hc.jian", sqlSessionFactoryRef = "jianFactory")})
public class DbConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.nong")
    public DruidDataSource nongDataSource() {
        return new DruidDataSource();
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.jian")
    public DruidDataSource jianDataSource() {
        return new DruidDataSource();
    }


    /**
     * ⼿动配置农⾏代理数据源
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceProxyXA nongXA(@Qualifier("nongDataSource") DruidDataSource dataSource){
        return new DataSourceProxyXA(dataSource);
    }

    /**
     * ⼿动代理建⾏数据源
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceProxyXA jianXA(@Qualifier("jianDataSource") DruidDataSource dataSource){
        return new DataSourceProxyXA(dataSource);
    }
    /**
     * 配置NongSqlSession
     */
    /*@Bean
    public SqlSessionFactory nongFactory(@Qualifier("nongDataSource") DruidDataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionFactory jianFactory(@Qualifier("jianDataSource") DruidDataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }*/

    /**
     * 配置NongSqlSession
     */
    @Bean
    public SqlSessionFactory nongFactory(@Qualifier("nongXA") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public SqlSessionFactory jianFactory(@Qualifier("jianXA") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }


    // 配置事务管理器
  /*  @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("jianDataSource") DataSource datasource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();

        transactionManager.setDataSource(datasource);
        return transactionManager;
    }*/
}
